package com.example.day0824_ui.slice.slice;

import com.example.day0824_ui.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class RegisterAbilitySlice extends AbilitySlice {
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP,0x00201,"MY_TAG");

    private static TextField tf1;           // 账号框
    private static TextField tf2;           // 密码框
    private static TextField tf3;           // 再次确认密码框
    private static Button btn_register;     //  注册框

    private static String[] strings = new String[2]; // 注册信息
    private static final Map<String,String> ADMI_PASSW = new HashMap<>(); // 注册的账号密码

    @Override
    public void onStart(Intent intent) {
        HiLog.info(LABEL,"RegisterAbilitySlice onStart");
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_register);

        BtnSetll();
    }
    private void BtnSetll() {
        HiLog.info(LABEL,"2");
        tf1 = (TextField)findComponentById(ResourceTable.Id_textf_regi_admi);
        tf2 = (TextField)findComponentById(ResourceTable.Id_textf_regi_psw);
        tf3 = (TextField)findComponentById(ResourceTable.Id_textf_regi_psw_2);
        btn_register = (Button) findComponentById(ResourceTable.Id_btn_reg_ability);

        btn_register.setClickedListener(this::BooleanAdmiPass);
    }

    private void BooleanAdmiPass(Component component) {



        if (ADMI_PASSW.keySet().contains(tf1.getText())) {
            HiLog.info(LABEL,"相等");
        }

        if ( tf2.getText().equals(tf3.getText()) && !tf1.getText().isEmpty() &&
              !tf2.getText().isEmpty() && !tf3.getText().isEmpty()  ) {

            HiLog.info(LABEL,"8");

            strings[0] = tf1.getText();
            strings[1] = tf2.getText();

            for (String admi: ADMI_PASSW.keySet()) {
                HiLog.info(LABEL,ADMI_PASSW.values() +admi);
                for (String s: strings) {
                    HiLog.info(LABEL,s);
                }
            }

            if (ADMI_PASSW.isEmpty()) {         // 如果ADMI_PASSW 为空，说明第一次添加账号

                ADMI_PASSW.put(tf1.getText(),tf2.getText());    // set集合添加 账号密码

                new ToastDialog(this).setText("注册成功").show();

            } else if (!ADMI_PASSW.isEmpty()){

                Boolean b = true;
                for (String admi: ADMI_PASSW.keySet()) {
                    HiLog.info(LABEL,"9");

                    // 如果set集合里面没有这个账号 则显示注册成功 否则失败
                    if (admi.equals(tf1.getText())) {
                         b = false;
                    }
                }
                if (b == true) {
                    new ToastDialog(this).setText("注册成功").show();
                    ADMI_PASSW.put(tf1.getText(),tf2.getText());
                    return;
                } else {
                    new ToastDialog(this).setText("注册失败").show();
                    return ;
                }
            }
        } else {

            new ToastDialog(this).setText("请正确填写信息").show();
            HiLog.info(LABEL,"10");
        }

    }

    @Override
    public void onActive() {
        HiLog.info(LABEL,"RegisterAbilitySlice onActive");
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
